<%
# Copyright (C) 2011 - present Instructure, Inc.
#
# This file is part of Canvas.
#
# Canvas is free software: you can redistribute it and/or modify it under
# the terms of the GNU Affero General Public License as published by the Free
# Software Foundation, version 3 of the License.
#
# Canvas is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
# A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
# details.
#
# You should have received a copy of the GNU Affero General Public License along
# with this program. If not, see <http://www.gnu.org/licenses/>.
%>

<%
  css_bundle :login_confirm
  js_bundle :registration_confirmation
  @headers = false
  @body_classes << "full-width modal body--login-confirmation"
  content_for :page_title, t(:page_title, "Register %{email}", :email => @communication_channel.path)
%>

<div id="modal-box" class="ic-Login-confirmation">
  <header class="ic-Login-confirmation__header">
    <%= image_tag("login/canvas-logo@2x.png", class:"ic-Login-confirmation__logo", alt:"Canvas by Instructure") %>
  </header>
  <div class="ic-Login-confirmation__content">
  <h2 class="ic-Login-confirmation__headline"><%= t 'titles.welcome_aboard', "Welcome Aboard!" %></h2>
  <% if @current_user && @current_user != @user && @user.registered? && @enrollment %>
    <p><%= mt 'details.wrong_account', "The invitation for %{course} was sent to **%{invited_user}**, but you are currently logged in as **%{current_user}**", :course => @course.name, :invited_user => @user.name, :current_user => @current_user.name %></p>
    <div class="button_box ic-Login-confirmation__actions">
      <a href="<%= registration_confirmation_path(@nonce, :enrollment => @enrollment.uuid, :transfer_enrollment => 1) %>" class="btn" id='transfer'><%=  t 'buttons.transfer_enrollment', "Transfer Enrollment" %></a>
      <a href="<%= login_url(:enrollment => @enrollment.uuid, :pseudonym_session => { :unique_id => @user.pseudonym.try(:unique_id) }, :expected_user_id => @user.id) %>" class="btn" id="login"><%=  t 'buttons.login_as', "Log in as %{invited_user}", :invited_user => @user.name %></a>
    </div>
  <% elsif !@merge_opportunities.empty? %>
    <div class="disambiguation_box" <%= hidden(true) if params[:register] %>>
      <% if @merge_opportunities.length == 1 && (@user.registered? || !@current_user || @current_user.communication_channels.active.email.by_path(@communication_channel.path).first) %>
        <div>
          <% if !@enrollment && @user.registered? %>
            <%= mt 'details.merge_one', "Would you like to combine the user accounts **%{current_user}** and **%{other_user}**?", :current_user => friendly_name(@user, @merge_opportunities), :other_user => friendly_name(@merge_opportunities.first.last.find { |p| p.account == @root_account } || @merge_opportunities.first.last.first, @merge_opportunities) %>
          <% else %>
            <%= mt 'details.account_exists', "There's a user account already associated with %{email}.

  **Are you %{name}?**", :email => @communication_channel.path, :name => friendly_name(@merge_opportunities.first.last.first, @merge_opportunities) %>
          <% end %></p>
        </div>
        <div class="button_box ic-Login-confirmation__actions">
          <% if @pseudonym %><a href="#" class="btn" id="register"><%= t 'buttons.no', 'No' %></a><% end %>
          <a href="<%= merge_or_login_link(@merge_opportunities.first.last.first) %>" class="btn not_external btn-primary" id="merge"><%= @user.registered? ? t('buttons.merge', "Combine") : t('buttons.yes', "Yes") %></a>
        </div>
        <% if !@current_user && @merge_opportunities.first.last.length > 1 %>
          </div><!-- Close #disambiguation_box -->
          <div id="where_to_log_in" style="display:none;">
            <p><strong><%=  t 'details.choose_login', "Select where you would like to log in at." %></strong></p>
            <ul class="unstyled_list">
              <% @merge_opportunities.first.last.each_with_index do |pseudonym, i| %>
                <li>
                  <label class="radio">
                    <input type="radio" name="pseudonym_select" value="<%= merge_or_login_link(pseudonym) %>"<%= ' checked="checked"' if i == 0 %>>
                    <%= pseudonym.account.name %> (<%= pseudonym.unique_id %>)
                  </label>
                </li>
              <% end %>
            </ul>
            <div class="button_box ic-Login-confirmation__actions">
              <a href="#" class="btn" id="back"><%= t 'buttons.back', "Back" %></a>
              <a href="#" class="btn btn-primary" id="merge_if_clicked"><%= t 'buttons.continue', 'Continue' %></a>
            </div>
          <!-- Purposely not closing div because it is closed below -->
        <% end %>
      <% elsif @merge_opportunities.length == 1 && @current_user %>
        <p><% if @enrollment %>
          <%= mt 'details.logged_in_other_user_course', "The invitation for %{course} was sent to **%{email}**, but you are currently logged in as **%{user}**.", :course => @course.name, :email => @communication_channel.path, :user => (@current_pseudonym && @current_pseudonym.user == @current_user ? @current_pseudonym : @current_user.pseudonym).unique_id %>
        <% else %>
          <%= mt 'details.logged_in_other_user', "**%{email}** is a new account, but you are currently logged in as **%{user}**.", :email => @communication_channel.path, :user => (@current_pseudonym && @current_pseudonym.user == @current_user ? @current_pseudonym : @current_user.pseudonym).unique_id %>
        <% end %></p>
        <p><strong><%=  t 'details.add_email', "Would you like to add this email address to your current account?" %></strong></p>
        <div class="button_box ic-Login-confirmation__actions">
          <a href="#" class="btn" id="register"><%= t 'buttons.new_account', 'No' %></a>
          <a href="<%= registration_confirmation_path(@nonce, :enrollment => @enrollment.try(:uuid), :confirm => 1) %>" class="btn btn-primary" id='merge'><%=  t 'buttons.add_email_address', "Yes, Add Email Address" %></a>
        </div>
      <% else %>
        <% if !@enrollment && @user.registered? %>
          <%= mt 'details.merge_multiple', "Select the account you would like to combine with **%{current_user}**.", :current_user => friendly_name(@user, @merge_opportunities) %>
        <% else %>
          <%= mt 'details.multiple_accounts_course', "There are already %{count} accounts associated with this email address.

Which is you?", :count => @merge_opportunities.length %>
        <% end %>
        <ul class="unstyled_list">
          <% @merge_opportunities.each_with_index do |(user, pseudonyms), i| %>
            <% if pseudonyms.length == 1 || user == @current_user %>
              <li>
                <label class="radio">
                  <input type="radio" name="pseudonym_select" value="<%= merge_or_login_link(pseudonyms.first) %>"<%= ' checked="checked"' if i == 0 %> />
                  <%= friendly_name(pseudonyms.first, @merge_opportunities) %>
                </label>
              </li>
            <% else %>
              <li><%= user.name %>
                <ul class="unstyled_list">
                  <% pseudonyms.each_with_index do |pseudonym, j| %>
                    <li>
                      <label class="radio">
                        <input type="radio" name="pseudonym_select" value="<%= merge_or_login_link(pseudonym) %>"<%= ' checked="checked"' if i == 0 && j == 0 %> />
                        <%= pseudonym.account.name %> (<%= pseudonym.unique_id %>)
                      </label>
                    </li>
                  <% end %>
                </ul>
              </li>
            <% end %>
          <% end %>
          <% if @pseudonym %>
            <li>
              <label class="radio">
                <input type="radio" name="pseudonym_select" value="new_user_account" />
                <%= t 'details.none_of_these', "None of these, I'm a new user" %>
              </label>
            </li>
          <% end %>
        </ul>
        <div class="button_box ic-Login-confirmation__actions">
          <a href="<%= merge_or_login_link(@merge_opportunities.first.last.first) %>" class="btn not_external" id="merge"><%= t 'buttons.continue', "Continue" %></a>
        </div>
      <% end %>
    </div>
  <% elsif @enrollment && @pseudonym %>
    <div class="disambiguation_box"<%= ' style="display: none;"'.html_safe if params[:register] %>>
      <p><%= mt 'details.account_needed', "You've been invited to join **%{course}**. To accept this request you need a Canvas account. Click the link below to create a Canvas account.", :course => @course.name %></p>
      <div class="button_box ic-Login-confirmation__actions">
        <a href="<%= login_url(:host => HostUrl.context_host(@root_account, @request.try(:host_with_port)), :confirm => @communication_channel.confirmation_code, :enrollment => @enrollment.uuid) %>" class="btn not_external"><%=  t 'buttons.have_account', "I Have a Canvas Account" %></a>
        <a href="#" class="btn" id="register"><%= t 'buttons.create_account', 'Create My Account' %></a>
      </div>
    </div>
  <% end %>

  <% if @pseudonym %>
    <%= form_for :pseudonym, :url => registration_confirmation_url(@nonce, request.query_parameters), :html => {:id => "registration_confirmation_form", :class => 'form-horizontal bootstrap-form', :style => ((!@merge_opportunities.empty? || @enrollment) && !params[:register]) ? hidden : '' } do |form| %>
      <p class="instructions"><%= if @enrollment
          mt 'details.register_with_course', "In order to finish signing you up for the course **%{course}**, we'll need a little more information.", :course => @course.name
        else
          t 'details.register', "In order to set up your account, we'll need a little more information."
        end %></p>
      <input type="hidden" name="register" value="1"/>
      <div class="control-group">
        <%= form.blabel :unique_id, :en => "Login", :class => 'control-label' %>
        <div class="controls" style="padding-top: 5px">
          <%= form.text_field :unique_id, :disabled => @pseudonym.unique_id.present? %>
        </div>
      </div>
      <div class="control-group">
        <%= form.blabel :password, :en => "Password", :class => 'control-label' %>
        <div class="controls">
          <%= form.password_field :password, :value => '' %>
        </div>
      </div>
      <div class="control-group">
        <%= blabel :user, :time_zone, :en => "Time Zone", :class => 'control-label' %>
        <div class="controls">
          <%= time_zone_select :user, :time_zone, I18nTimeZone.us_zones, :model => I18nTimeZone, :default => @pseudonym.account.default_time_zone %>
        </div>
      </div>
      <% if @pseudonym.account == Account.default %>
      <div class="control-group">
        <div class="controls">
          <label for="user_subscribe_to_emails" class="checkbox">
            <%= check_box :user, :subscribe_to_emails, :checked => true %>
            <%= t "labels.subscribe_to_emails", "I want to receive information, news and tips from Instructure" %>
          </label>
        </div>
      </div>
      <% end %>
      <div class="control-group">
        <div class="controls">
          <% if @root_account.require_acceptance_of_terms?(@user) %>
            <% TermsOfService.ensure_terms_for_account(@root_account) %>
            <% js_env(:TERMS_OF_SERVICE_CUSTOM_CONTENT => @root_account.terms_of_service.terms_of_service_content&.content) %>
            <% js_bundle :terms_of_service_modal %>
            <div class="terms-of-service__register">
              <label for="user_terms_of_use" class="checkbox">
                <%= check_box :user, :terms_of_use %>
                {{#t}}I agree to the{{/t}}
              </label>
              <a href="#" class="terms_of_service_link terms-of-service__link"></a>
            </div>
          <% end %>
        </div>
      </div>
      <div class="control-group">
        <div class="controls">
          <% unless @merge_opportunities.empty? %>
            <a href="#" class="btn" id="back"><%= t 'buttons.back', "Back" %></a>
          <% end %>
          <button type="submit" class="btn btn-primary"><%= t 'buttons.register', "Register" %></button>
        </div>
      </div>
    <% end %>
  <% end %>
  </div>
</div>
